package iv1201project.view;

import iv1201project.controller.ApplicantDAO;
import iv1201project.model.*;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * The class responsible for recieving user-session information, connected to the
 * application submission process.
 */
@Named("applicantmanager")
@SessionScoped
public class ApplicantManager implements Serializable{

    @EJB
    private ApplicantDAO applicantDAO;
    @Inject
    private ApplicantDTO applicantDTO;
    private Competence areaofexp;
    private String yearsofexp;
    private ArrayList<Competence> areaofexparr = new ArrayList<Competence>();
    private ArrayList<BigDecimal> yearsofexparr = new ArrayList<BigDecimal>();
    private String fromAvail;
    private String toAvail;
    private ArrayList<String> fromDateAvail = new ArrayList<String>();
    private ArrayList<String> toDateAvail = new ArrayList<String>();
    private String areaofexpstring = "";
    private String availabilitystring = "";
    private List<Competence> competencearr = new ArrayList<Competence>();

    /**
     * Adds an applicant by calling the controllers addApplicant method.
     * @throws IOException Thrown when a webpage isn't found on the server.
     */
    @Logthis
    public String addApplicant() {
        try {
            if(finalCheck()){
            applicantDAO.addApplicant(applicantDTO);
            cancel();
            return "case1";
            } else {
                return "error";
            }
        } catch (Exception e) {
            return "error";
        }
    }

    /**
     * Adds area of expertise to a temprary arraylist.
     */
    public void addAOE() {
        areaofexparr.add(areaofexp);
        yearsofexparr.add(new BigDecimal(yearsofexp));
        areaofexpstring += "<br />Area: " + areaofexp.getName() + " Years: " + yearsofexp;
    }

    /**
     * Adds availability to a temprary arraylist.
     */
    public void addAvailability() {
        fromDateAvail.add(fromAvail);
        toDateAvail.add(toAvail);
        availabilitystring += "<br />" + fromAvail + " -> " + toAvail;
    }

    /**
     * Cancels the login and redirects to the first page.
     * @throws IOException Thrown when a webpage isn't found on the server.
     */
    public String cancel() throws IOException {
        applicantDTO = new ApplicantDTO();
        areaofexparr = new ArrayList<Competence>();
        yearsofexparr = new ArrayList<BigDecimal>();
        fromDateAvail = new ArrayList<String>();
        toDateAvail = new ArrayList<String>();
        availabilitystring = "";
        areaofexpstring = "";
        return "case2";
    }

    /**
     * Forwards the user to the next page.
     * @throws IOException Thrown when a webpage isn't found on the server.
     */
    public String nextPage1() {
        return "case1";
    }

    /**
     * Forwards the user to the next page.
     * @throws IOException Thrown when a webpage isn't found on the server.
     */
    public String nextPage2() {
        return "case1";
    }

    /**
     * Forwards the user to the next page.
     * @throws IOException Thrown when a webpage isn't found on the server.
     */
    public String nextPage3() {
        applicantDTO.setFromDateAvail(fromDateAvail);
        applicantDTO.setToDateAvail(toDateAvail);
        applicantDTO.setAreaOfExp(areaofexparr);
        applicantDTO.setYearsOfExp(yearsofexparr);
        return "case1";
    }
    
    private boolean finalCheck(){
        if(applicantDTO.getName() == null || applicantDTO.getEmail() == null ||
           applicantDTO.getSurname() == null || applicantDTO.getFromDateAvail() == null || 
           applicantDTO.getToDateAvail() == null)
            return false;
        else
            return true;
    }

    /**
     * @param areaofexp The area of expertise of the applicant.
     */
    public void setAreaofexp(String areaofexp) {
        for(Competence c : competencearr){
            if(c.getName().equalsIgnoreCase(areaofexp)){
                this.areaofexp = c;
                break;
            }
        }
    }

    /**
     * @return The area of expertise of the applicant.
     */
    public String getAreaofexp() {
        if(areaofexp == null)
            return "asdfgh";
        else
            return areaofexp.getName();
    }

    /**
     * @param years The years of experience of the applicant.
     */
    public void setYearsofexp(String years) {
        this.yearsofexp = years;
    }

    /**
     * @return The years of experience of the applicant.
     */
    public String getYearsofexp() {
        return yearsofexp;
    }

    /**
     * @return The availability of the applicant.
     */
    public String getFromAvail() {
        return fromAvail;
    }

    /**
     * @param availability The availability of the applicant.
     */
    public void setFromAvail(String availability) {
        this.fromAvail = availability;
    }

    /**
     * @return the areaofexpstring
     */
    public String getAreaofexpstring() {
        return areaofexpstring;
    }

    /**
     * @param areaofexpstring the areaofexpstring to set
     */
    public void setAreaofexpstring(String areaofexpstring) {
        this.areaofexpstring = areaofexpstring;
    }

    /**
     * @return the availabilitystring
     */
    public String getAvailabilitystring() {
        return availabilitystring;
    }

    /**
     * @param availabilitystring the availabilitystring to set
     */
    public void setAvailabilitystring(String availabilitystring) {
        this.availabilitystring = availabilitystring;
    }

    /**
     * @return the applicantDTO
     */
    public ApplicantDTO getApplicantDTO() {
        return applicantDTO;
    }

    /**
     * @param applicantDTO the applicantDTO to set
     */
    public void setApplicantDTO(ApplicantDTO applicantDTO) {
        this.applicantDTO = applicantDTO;
    }

    /**
     * @return the toDateAvail
     */
    public ArrayList<String> getToDateAvail() {
        return toDateAvail;
    }

    /**
     * @param toDateAvail the toDateAvail to set
     */
    public void setToDateAvail(ArrayList<String> toDateAvail) {
        this.toDateAvail = toDateAvail;
    }

    /**
     * @return the toAvail
     */
    public String getToAvail() {
        return toAvail;
    }

    /**
     * @param toAvail the toAvail to set
     */
    public void setToAvail(String toAvail) {
        this.toAvail = toAvail;
    }
    
    /**
     * Get a list of competence to be set in the drop down menu.
     * 
     * @return A list of SelectedItems.
     */
    public List<SelectItem> getCompetence() {
        competencearr = applicantDAO.getComptence();
        ArrayList<SelectItem> retval = new ArrayList<SelectItem>();
        
        for (Competence c : competencearr) {
            retval.add(new SelectItem(c.getName()));
        }
        return retval;
    }
}
